package com.ruoyi.web.web.form;

import com.ruoyi.web.core.config.AppConf;
import com.ruoyi.accountant.domain.Account;
import com.ruoyi.accountant.domain.Currency;
import com.ruoyi.accountant.domain.Reason;
import com.ruoyi.accountant.service.AccountService;
import com.ruoyi.accountant.service.CurrencyService;
import com.ruoyi.accountant.service.ReasonService;
import lombok.Data;
import org.springframework.util.StringUtils;

import javax.servlet.http.HttpSession;
import java.util.Calendar;
import java.util.Date;

/***
 * 收入表单
 */
@Data
public class IncomeForm implements Validatable {
    private Long account; // 账户ID
    private String transactionType; // 交易类型
    private Integer currency; // 币种类型
    private Date incomeDate = new Date(); // 收款日期
    private Date proofDate = new Date(); // 凭证日期
    private Long reasonId; //原因
    private String object; //收入来源对象
    private Double money; // 收入金额
    private Double serviceCharge = 0D; // 手续费
    private String comment; //备注
    private String taxRateType; // 税率类型
    private Double taxRate; // 税率
    private String tag;
    private String attachment; //
    private String invoice; // 发票
    private Double deposit; // 订金
    private Long ofCompany;

    private String accountName; // 账户名称
    private String reasonName; // 原因名称
    private String currencyName; //货币名称

    public Validation validate(boolean api, AccountService accountService,
                               CurrencyService currencyService,
                               ReasonService reasonService, HttpSession session) {
        // 附件
        if(!StringUtils.isEmpty(attachment)) {
            String file = (String) session.getAttribute(attachment);
            if(StringUtils.isEmpty(file)) {
                Validation.error("文件不存在");
            }
            session.removeAttribute(attachment);
            attachment = file;
        }

        if(api) { // 是api调用方式
            if(StringUtils.isEmpty(accountName)) {
                return Validation.error("账户名错误");
            } else if(StringUtils.isEmpty(currencyName)) {
                return Validation.error("币种不能为空");
            } else if(StringUtils.isEmpty(reasonName)) {
                return Validation.error("原因不能为空");
            }

            Account account = accountService.findByAname(accountName);
            Currency currency = currencyService.findByEnName(currencyName);
            Reason reason = reasonService.findByRname(reasonName);

            if(account == null) {
                return Validation.error("账户不存在");
            } else if(currency == null) {
                return Validation.error("币种不存在");
            } else if(reason == null || reason.getLevel() != 3) {
                return Validation.error("原因不存在或不是具体原因");
            }

            if(!reason.getRtype().equals("收入")) {
                return Validation.error("原因类型不适用");
            }

            this.account = account.getAid();
            this.currency = currency.getCid();
            this.reasonId = reason.getRid();
        } else {
            if(this.currency == null) {
                return Validation.error("币种不能为空");
            } else if(this.account == null) {
                return Validation.error("账户不能为空");
            } else if(this.reasonId == null) {
                return Validation.error("原因不能为空");
            }

            Currency currency = currencyService.findById(this.currency);
            Account account = accountService.findById(this.account);
            Reason reason = reasonService.findById(this.reasonId);

            if(currency == null) {
                return Validation.error("币种不存在");
            } else {
                this.currencyName = currency.getEnName();
            }

            if(account == null) {
                return Validation.error("账户不存在");
            }

            if(reason == null || reason.getLevel() != 3) {
                return Validation.error("原因不存在或不是具体原因");
            }

            if(!reason.getRtype().equals("收入")) {
                return Validation.error("原因类型不适用");
            }
        }

        // 日期
        if(transactionType.equals("同日收入")) { // 如果是同日收入，凭证日期和实际收款日期相同
            proofDate = incomeDate;

            // 收款日期不能为空，并且不能大于当前日期
            Calendar instance = Calendar.getInstance();
            instance.add(Calendar.DAY_OF_MONTH, 1);

            if(this.incomeDate == null || incomeDate.compareTo(instance.getTime()) == 1 || incomeDate.compareTo(instance.getTime()) == 0) {
                return Validation.error("收款日期错误");
            }

        } else if(transactionType.equals("应收款")) { // 需要输入凭证日期，收款日期为空
            incomeDate = null;
            if(proofDate == null) {
                return Validation.error("应收款需要输入凭证日期");
            }
        } else if(transactionType.equals("非同日收入")) {
            if(proofDate == null || incomeDate == null) {
                return Validation.error("非同日收入需要输入收款日期和凭证日期");
            }
        } else {
            return Validation.error("交易类型错误");
        }

        //账户检查
        if(money == null || money <= 0) {
            return Validation.error("金额错误");
        }

        if(serviceCharge == null || serviceCharge < 0) {
            return Validation.error("手续费错误");
        }

        if(taxRateType == null || !taxRateType.equals("应税") && !taxRateType.equals("免税") && !taxRateType.equals("零税率")) {
            return Validation.error("税率类型错误");
        }

        if(taxRateType.equals("应税") && taxRate <= 0) {
            return Validation.error("应税率错误:"+taxRate);
        }


        if(deposit != null && deposit < 0) {
            return Validation.error("订金不能为负数");
        } else if(deposit != null && (reasonService.findById(reasonId).getRname().equals("销货收入")) && (!transactionType.equals("同日收入")) ) {
            return Validation.error("订金仅支持销货收入且交易类型必须为同日收入");
        } else if(deposit != null && deposit >= money) {
            return Validation.error("订金必须小于总额");
        }

        // 账户不是停用状态
        Account byId = accountService.findById(account);
        if(byId.getStop() == AppConf.ON) {
            return Validation.error(byId.getAname()+"已经停用");
        }

        return Validation.ok();
    }
}
